//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartPointView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartPointView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAX_POINT_SERIES		4
#define MAX_POINTS_IN_SERIES	6
#define MIN_Y_VAL				0.
#define MAX_Y_VAL				20.

#define X_RANGE_PER_SERIES		3
#define X_VALUE_GRADE			100.

/////////////////////////////////////////////////////////////////////////////
// CChartPointView

IMPLEMENT_DYNCREATE(CChartPointView, CBCGPChartExampleView)

CChartPointView::CChartPointView()
	: CBCGPChartExampleView(CChartPointView::IDD)
{
	//{{AFX_DATA_INIT(CChartPointView)
	m_nMarkerSize = 0;
	m_nMarkerShape = 0;
	m_bShowDataLabels = TRUE;
	m_nDataLabelAngle = 3;
	m_nCrossType = 0;
	//}}AFX_DATA_INIT
}

CChartPointView::~CChartPointView()
{
}

void CChartPointView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartPointView)
	DDX_Control(pDX, IDC_DATA_LABEL_ANGLE, m_wndDataLabelAngle);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SIZE, m_nMarkerSize);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SHAPE, m_nMarkerShape);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_ANGLE, m_nDataLabelAngle);
	DDX_CBIndex(pDX, IDC_CROSS_TYPE, m_nCrossType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartPointView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartPointView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SHAPE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SIZE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_ANGLE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_CROSS_TYPE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartPointView diagnostics

#ifdef _DEBUG
void CChartPointView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartPointView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartPointView message handlers

void CChartPointView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartLine, BCGP_CT_SIMPLE);
	pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);

	for (int i = 0; i < MAX_POINT_SERIES; i++)
	{
		int nXRangeLow = (int)(i - MAX_POINT_SERIES / 2.) * X_RANGE_PER_SERIES;
		int nXRangeHigh = nXRangeLow + X_RANGE_PER_SERIES;

		for (int j = 0; j < MAX_POINTS_IN_SERIES; j++)
		{
			double dblX = Rand(nXRangeLow / X_VALUE_GRADE, nXRangeHigh / X_VALUE_GRADE);
			double dblY = Rand(MIN_Y_VAL, MAX_Y_VAL);
			pChart->AddChartDataYX(dblY, dblX, i);
		}

		CBCGPChartSeries* pSeries = pChart->GetSeries(i);
		ASSERT_VALID(pSeries);

		BCGPChartFormatSeries style = pSeries->GetSeriesFormat();
		style.m_curveType = BCGPChartFormatSeries::CCT_NO_LINE;

		pSeries->SetSeriesFormat(style);

		pSeries->SetDataLabelDataFormat(_T("%.2f"));
	}

	CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
	ASSERT_VALID(pYAxis);

	pYAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;
	pYAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_CROSS;

	pYAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;

	CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
	ASSERT_VALID(pXAxis);

	pXAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_OUTSIDE;
	pXAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;

	OnUpdateChart();
}

void CChartPointView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelAngle.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataMarkers(TRUE, max(7, 5 * (m_nMarkerSize + 1)), 
		(BCGPChartMarkerOptions::MarkerShape)m_nMarkerShape);

	double dblDataLabelAngle = (m_nDataLabelAngle - 2) * 45;
	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, TRUE, dblDataLabelAngle);

	CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
	ASSERT_VALID(pYAxis);

	CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
	ASSERT_VALID(pXAxis);

	switch (m_nCrossType)
	{
	case 0:
		pYAxis->m_crossType = CBCGPChartAxis::CT_AUTO;
		pXAxis->m_crossType = CBCGPChartAxis::CT_AUTO;
		break;

	case 1:
		pYAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
		pXAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
		break;

	case 2:
		pYAxis->m_crossType = CBCGPChartAxis::CT_MINIMUM_AXIS_VALUE;
		pXAxis->m_crossType = CBCGPChartAxis::CT_MINIMUM_AXIS_VALUE;
		break;

	case 3:
		pYAxis->m_crossType = CBCGPChartAxis::CT_AXIS_VALUE;
		pYAxis->m_dblCrossOtherAxisValue = 0.02;

		pXAxis->m_crossType = CBCGPChartAxis::CT_AXIS_VALUE;
		pXAxis->m_dblCrossOtherAxisValue = 7.0;
		break;
	}

	pChart->Redraw();
}
